#include "opedb.h"
#include <QSqlError>

OpeDB::OpeDB(QObject *parent)
    : QObject{parent}
{
    m_db = QSqlDatabase::addDatabase("QSQLITE");
}

OpeDB::~OpeDB()
{
    m_db.close();
}

bool OpeDB::handleRedist(const QString name, const QString pwd)
{
    if(name.isEmpty()||pwd.isEmpty()){
        qDebug()<<"name or password is NULL";
        return false;
    }
    QString data = QString("insert into usrInfo(name,pwd) values(\'%1\',\'%2\')").arg(name).arg(pwd);
    QSqlQuery query;
    return query.exec(data);

}

bool OpeDB::handleLogin(const QString name, const QString pwd)
{
    if(name.isEmpty()||pwd.isEmpty()){
        qDebug()<<"name or password is NULL";
        return false;
    }
    QString data = QString("select * from usrInfo where name=\'%1\' and pwd=\'%2\' and online = 0").arg(name).arg(pwd);
    qDebug()<<data;
    QSqlQuery query;
    query.exec(data);
    if(query.next()){
        data = QString("update usrInfo set online=1 where name=\'%1\' and pwd=\'%2\' and online = 0").arg(name).arg(pwd);
        qDebug() << data;
        QSqlQuery query;
        query.exec(data);
        return true;
    }else{
        return false;
    }
}

void OpeDB::handleOffline(const char *name)
{
    if(NULL == name){
        qDebug() << "name is NULL";
        return;
    }
    QString data = QString("update usrInfo set online=0 where name=\'%1\'").arg(name);
    QSqlQuery query;
    query.exec(data);
}

QStringList OpeDB::handleAllOnline()
{
    QString data = QString("Select name from usrInfo where online = 1");
    QSqlQuery query;
    query.exec(data);
    QStringList result;
    result.clear();

    while(query.next())
    {
        result.append(query.value(0).toString());
    }
    return result;
}

int OpeDB::handleSearchUsr(const char *name)
{
    if(NULL == name)
    {
        return -1;
    }
    QString data = QString("select online from usrInfo where name=\'%1\'").arg(name);
    QSqlQuery query;
    query.exec(data);
    if(query.next())
    {
        int ret = query.value(0).toInt();
        return ret;
    }
    else
    {
        return -1;
    }
}

int OpeDB::handleAddFriend(const char *pername, const char *name)
{
    qDebug()<<pername<<" "<<name;
    if(NULL == pername || NULL == name){
        return -1;
    }
    QString data = QString(
                       "SELECT * FROM friend "
                       "WHERE (id = (SELECT id FROM usrInfo WHERE name='%1') AND friendId = (SELECT id FROM usrInfo WHERE name='%2')) "
                       "OR (id = (SELECT id FROM usrInfo WHERE name='%2') AND friendId = (SELECT id FROM usrInfo WHERE name='%1'))"
                       ).arg(pername).arg(name);
    QSqlQuery query;
    query.exec(data);

    if(query.next())
    {
        return 2;
    }else{
        data = QString("select online from usrInfo where name=\'%1\'").arg(name);
        query.exec(data);
        if(query.next())
        {
            int ret = query.value(0).toInt();
            return ret;
        }
        else
        {
            return 3;
        }
    }


}

int OpeDB::handleAgreeAddFriend(const char *pername, const char *name)
{
    QString data = QString("INSERT INTO friend(id, friendId) "
                   "SELECT u1.id, u2.id FROM usrInfo u1, usrInfo u2 "
                   "WHERE u1.name = '%1' AND u2.name = '%2'").arg(pername).arg(name);
    QSqlQuery query;
    if (!query.exec(data)) {
        qDebug() << "添加失败：" << query.lastError().text();
        return -1;
    } else {
        qDebug() << "添加成功";
        return 1;
    }
}

QStringList OpeDB::handleFlushFriend(const char *name)
{
    QStringList strFriendList;
    strFriendList.clear();
    if(NULL == name)
    {
        return strFriendList;
    }
    QString data = QString("select name from usrInfo where online = 1 "
                           "and (id in (select id from friend where friendId = (select id from usrInfo where name = \'%1\'))"
                           "or id in (select friendId from friend where id = (select id from usrInfo where name = \'%1\'))) ").arg(name);
    QSqlQuery query;
    query.exec(data);
    while(query.next())
    {
        strFriendList.append(query.value(0).toString());
    }
    return strFriendList;
}

bool OpeDB::handleDelFriend(const char *name, const char *friendName)
{
    if(NULL == name || NULL == friendName){
        return false;
    }
    QString data = QString("delete from friend where"
                           "(id = (select id from usrInfo where name = \'%1\') and friendId = (select id from usrInfo where name = \'%2\'))"
                           "or (id = (select id from usrInfo where name = \'%2\') and friendId = (select id from usrInfo where name = \'%1\'))").arg(name).arg(friendName);
    QSqlQuery query;
    query.exec(data);
    return true;
}

OpeDB &OpeDB::getInstance()
{
    static OpeDB instance;
    return instance;
}

void OpeDB::init()
{
    m_db.setHostName("localhost");
    //使用了绝对路径
    m_db.setDatabaseName(".\cloud.db");
    if(m_db.open())
    {
        QSqlQuery query;
        query.exec("select * from usrInfo");
        while(query.next()){
            QString data = QString("%1,%2,%3").arg(query.value(0).toString()).arg(query.value(1).toString()).arg(query.value(2).toString());
            qDebug()<<data;
        }
    }else{
        QMessageBox::critical(NULL,"打开数据库","打开数据库失败");
    }
}
